//
//  SZLWineCultureViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/28.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLWineCultureViewController.h"
#import "SDCycleScrollView.h"
#import "CultureToolBar.h"
#import "CultureTableViewCell.h"
#import "CultureNetService.h"
#import "MBProgressHUD+MJ.h"
#import <MJRefresh.h>
#import "SZLArticleDetailViewController.h"
#import "HomeNetService.h"
#import "BannerModel.h"


@interface SZLWineCultureViewController ()<UITableViewDataSource,UITableViewDelegate,CultureToolBarDelegate>

@property (nonatomic,weak) SDCycleScrollView *roolView;
@property (nonatomic,strong) CultureToolBar *toolBar;
@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,strong) NSMutableArray *sources;
@property (nonatomic,strong) NSMutableArray *banners;

@property (nonatomic,strong) NSDictionary *listDict;//menu列表
@property (nonatomic,assign) NSInteger curPage;//当前页
@property (nonatomic,copy) NSString *categoryId;

@end

@implementation SZLWineCultureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"红酒文化";
    self.view.backgroundColor = [UIColor whiteColor];
    //self.navigationController.navigationBar.clipsToBounds = YES;
    self.curPage = 1;
    self.categoryId = @"";
    [self setTableView];
    [self getBannerList];
    [self getMenuLists];
    [self getArticleList];
}
- (void)setTableView
{
    SDCycleScrollView *roolView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0, ScreenW, 350*Scale) delegate:nil placeholderImage:[UIImage imageNamed:@"default_large_goods_image"]];
    roolView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    roolView.autoScrollTimeInterval = 3.5f;
    self.roolView = roolView;
    self.sourceTab.tableHeaderView = roolView;
    __WEAK
    roolView.clickItemOperationBlock = ^(NSInteger index){
        //轮播图点击
//        BannerModel *model = weakSelf.banners[index];
//        if (model.bannerToPage == 1) {
//            SZLGoodsDetailViewController *detailVc = [[SZLGoodsDetailViewController alloc]init];
//            detailVc.productNo = model.ID;
//            detailVc.type = GoodsTypeFutures;
//            [self.navigationController pushViewController:detailVc animated:YES];
//        }else if (model.bannerToPage == 2){
//            SZLGoodsDetailViewController *detailVc = [[SZLGoodsDetailViewController alloc]init];
//            detailVc.productNo = model.ID;
//            detailVc.type = GoodsTypeFreeWine;
//            [self.navigationController pushViewController:detailVc animated:YES];
//        }else if (model.bannerToPage == 3){
//            SZLGoodsDetailViewController *detailVc = [[SZLGoodsDetailViewController alloc]init];
//            detailVc.productNo = model.ID;
//            detailVc.type = GoodsTypeNormal;
//            [self.navigationController pushViewController:detailVc animated:YES];
//        }else if (model.bannerToPage == 4){
//            SZLArticleDetailViewController *webVc = [[SZLArticleDetailViewController alloc]init];
//            webVc.ID = model.ID;
//            [self.navigationController pushViewController:webVc animated:YES];
//        }else if (model.bannerToPage == 5){
//            SZLTranstionViewController *transtioniVc = [[SZLTranstionViewController alloc]init];
//            [self.navigationController pushViewController:transtioniVc animated:YES];
//        }

    };
    [self.view addSubview:self.sourceTab];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.toolBar.delegate = self;
    return self.toolBar;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CultureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CultureTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SZLArticleDetailViewController *webVc = [[SZLArticleDetailViewController alloc]init];
    ArticleListModel *model = self.sources[indexPath.row];
    webVc.ID = [NSString stringWithFormat:@"%zd",model.ID];
    [self.navigationController pushViewController:webVc animated:YES];
}
#pragma mark ----------toolBarDelegate-------------
- (void)cultureToolBarClick:(MenuListModel *)model
{
    self.categoryId = [NSString stringWithFormat:@"%zd",model.ID];
    [self getArticleList];
}
- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenW, ScreenH-64)];
        _sourceTab.dataSource = self;
        _sourceTab.delegate = self;
        _sourceTab.rowHeight = 120;
        _sourceTab.sectionHeaderHeight = 35;
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([CultureTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([CultureTableViewCell class])];
        _sourceTab.mj_footer = _sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.curPage++;
            [self getArticleList];
        }];;
        _sourceTab.tableFooterView = [[UIView alloc]init];
    }
    return _sourceTab;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}
- (void)setToolBar
{
    [self.view addSubview:self.toolBar];
}
- (CultureToolBar *)toolBar
{
    if (!_toolBar) {
        _toolBar = [[CultureToolBar alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 35)];
    }
    return _toolBar;
}
- (NSMutableArray *)banners
{
    if (!_banners) {
        _banners = [NSMutableArray array];
    }
    return _banners;
}
#pragma mark ----------------接口相关-----------------------
- (void)getBannerList
{
    [HomeNetService getBannerListWithParame:@{@"bannerType":@2,@"isNew":@1} response:^(BOOL success, NSString *msg,NSArray *banners) {
        if (success) {
            [self.banners addObjectsFromArray:banners];
            NSMutableArray *urls = [NSMutableArray array];
            for (BannerModel *model in banners) {
//                [urls addObject:model.bannerUrl];
            }
            self.roolView.imageURLStringsGroup = urls;
        }
    }];
}
- (void)getMenuLists
{
    [self showWaiter];
    [CultureNetService getCultureMenuWithParame:nil response:^(BOOL success, NSString *msg, NSDictionary *listDict) {
        [self hideWaiter];
        if (success) {
            self.toolBar.listDict = listDict;
        }
    }];
}
- (void)getArticleList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.curPage);
    parame[@"categoryId"] = self.categoryId;
    [CultureNetService articleListsWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        if (success) {
            [self.sourceTab.mj_footer endRefreshing];
            self.sourceTab.mj_footer.hidden = (lists.count<20)?YES:NO;
            [self hideNoDataLab];
            if (self.curPage == 1) {
                [self.sources removeAllObjects];
                if (lists.count == 0) {
                    [self.sourceTab reloadData];
                    [self showNoDataLab:self.sourceTab range:CGRectMake(0, 235, ScreenW, self.sourceTab.height-235) title:@"没有相关文章"];
                    return ;
                }
            }else if (lists.count<1&&self.curPage>1){
                [MBProgressHUD showSuccess:@"没有更多数据了" toView:self.view];
                return ;
            }
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}


@end
